package com.ycsj.goldmedalnewconcept.master.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.master.adapter.PackLvAdapter;
import com.ycsj.goldmedalnewconcept.master.bean.PackInfo;
import com.ycsj.goldmedalnewconcept.view.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 红包记录
 */
@SuppressLint("NewApi")
public class MaseterRedPackageRecordActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    //正常请求
    public static final int NORMAL = 0X110;

    //下拉刷新
    public static final int PULLTOREFRESH = 0X120;

    //返回
    private ImageView iv_back;

    //加载视图
    private LinearLayout llLoading;

    //空数据
    private LinearLayout llEmpty;

    //下拉刷新控件
    private PullToRefreshLayout ptrl;

    //列表控件
    private ListView lv;

    //头布局
    private View headView;

    //头布局的布局控件
    private TextView tvSend;//总发出

    private TextView tvNum;//总领取人数

    private TextView tvUse;//总使用

    //标题
    private TextView tv_title;

    //列表控件的数据集合
    private List<PackInfo.RedPacketsBean> oldDatas;

    private PackLvAdapter adapter;

    //加载框
    private RelativeLayout rlLoading;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_red_package_record;
    }

    @Override
    protected void init() {

        //状态栏
        initStateBar();

        //初始化View的方法
        initViews();

        //初始化列表
        initLv();

        //设置监听
        initListener();

        //网络请求数据
        http(NORMAL);
    }


    /**
     * 网络请求的方法
     *
     * @param method
     */
    private void http(final int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "6");
        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("schoolId", AccountUtils.getSchoolId(this));
//        map.put("apptoken", AccountUtils.getToken(this));
//        map.put("platform", "2");

        HttpUtils.post(HttpAddress.PARENTREDLIST, map, this, new HttpCallBack<PackInfo>() {
            @Override
            public void onSuccess(PackInfo info) {
                rlLoading.setVisibility(View.GONE);
                if ("1".equals(info.getState())) {
                    //设置页面数据

                    final List<PackInfo.RedPacketsBean> newDatas = info.getRedPackets();
                    if (null != newDatas && newDatas.size() != 0) {

                        Collections.reverse(newDatas);//倒序处理

                        //设置头部数据
                        tvSend.setText(info.getTotleMoney() + "");

                        tvNum.setText(info.getTotleRobbed() + " 人");

                        tvUse.setText(info.getTotleUsed() + "");

                        if (method == NORMAL) {
                            //正常请求
                            if (llLoading != null) {
                                llLoading.setVisibility(View.GONE);
                            }
                            oldDatas.addAll(newDatas);

                            lv.setAdapter(adapter);
                        } else {
                            //下拉刷新
                            oldDatas.clear();
                            oldDatas.addAll(newDatas);
                            ptrl.refreshFinish(PullToRefreshLayout.REFRESH_SUCCEED);
                            adapter.notifyDataSetChanged();
                        }
                    } else {
                        if (method == NORMAL) {
                            //正常请求没有数据时设置默认数据
                            tvSend.setText("0");
                            tvNum.setText("0 人");
                            tvUse.setText("0");
                            //正常加载
                            if (llLoading != null) {
                                llLoading.setVisibility(View.GONE);
                            }
                            if (llEmpty != null) {
                                llEmpty.setVisibility(View.VISIBLE);
                            }
                            lv.setAdapter(adapter);
                        } else {
                            //下拉刷新
                            ptrl.refreshFinish(PullToRefreshLayout.REFRESH_FAIL);
                        }
                    }
                } else {
                    ToastUtils.showShort(BaseApplication.STATEERROE + info.getState());
                }
            }

            @Override
            public void onError(String e) {
                rlLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });
    }

    /**
     * 初始化View的方法
     */
    private void initViews() {
        iv_back = findViewById(R.id.iv_back);
        llLoading = findViewById(R.id.ll_loading);
        ptrl = findViewById(R.id.refresh_view);
        lv = findViewById(R.id.lv);
        llEmpty = findViewById(R.id.ll_empty);
        headView = View.inflate(this, R.layout.lv_headview, null);
        tvSend = headView.findViewById(R.id.tv_send);
        tvNum = headView.findViewById(R.id.tv_num);
        tvUse = headView.findViewById(R.id.tv_use);
        rlLoading = findViewById(R.id.rl_loading);
        tv_title = findViewById(R.id.tv_title);

        tv_title.setText("红包记录");
    }


    /**
     * 初始化列表控件的方法
     */
    private void initLv() {
        oldDatas = new ArrayList<>();
        adapter = new PackLvAdapter(this, oldDatas, R.layout.item_lv_pack);
        //给ListView添加头布局
        lv.addHeaderView(headView);
    }

    /**
     * 设置监听的方法
     */
    private void initListener() {

        iv_back.setOnClickListener(this);

        ptrl.setOnRefreshListener(this);

        //条目点击事件
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if (position == 0) {
                    return;
                }

                // 跳转到红包领取
                Intent intent = new Intent(MaseterRedPackageRecordActivity.this,
                        MasterRedPackageGetRecordActivity.class);

                intent.putExtra("redid", oldDatas.get(position - 1).getRedId() + "");

                intent.putExtra("title", oldDatas.get(position - 1).getTitle() + "");

                intent.putExtra("Money", oldDatas.get(position - 1).getMoney() + "");

                startActivity(intent);
            }
        });
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        http(PULLTOREFRESH);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
        }
    }
}
